package 题目集.线段树or树状数组.线段树;

import java.io.*;

/**
 * 维护不一样的信息
 * https://www.luogu.com.cn/problem/P3870
 * 题意：
 *  两个操作，
 *  1：改变区间[l,r]的灯
 *  2.查询[l,r]开着的灯
 * 思路：
 *      维护区间内开着的灯的数量，懒住操作，懒两次直接抵消
 */
public class ch08_开关 {
    static int n, m;
    static Node[] tree;
    static PrintWriter pw=new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));

    public static void main(String[] args) {
        Reader sc = new Reader();
        n = sc.nextInt();
        m = sc.nextInt();
        int tn = (int) Math.pow(2, 1 + Math.ceil(Math.log(n) / Math.log(2)));
        tree = new Node[tn];
        build(1, 1, n);
        for (int i = 0; i < m; i++) {
            int op = sc.nextInt();
            int l = sc.nextInt();
            int r = sc.nextInt();
            if (op == 0) {
                touch(1, l, r);
            } else {
                int query = query(1, l, r);
                pw.println(query);
            }
        }
        pw.flush();
    }

    private static void build(int i, int l, int r) {
        tree[i] = new Node(l, r);
        if (l == r) return;
        int m = l + r >> 1;
        build(i << 1, l, m);
        build(i << 1 | 1, m + 1, r);
    }

    private static void touch(int i, int l, int r) {
        if (isLazy(i, l, r)) {
            touchLazy(i);
            return;
        }
        push(i);
        int m = tree[i].l + tree[i].r >> 1;
        if (l <= m) touch(i << 1, l, r);
        if (m < r) touch(i << 1 | 1, l, r);
        pull(i);
    }

    private static void pull(int i) {
        tree[i].sum = tree[i << 1].sum + tree[i << 1 | 1].sum;
    }

    public static int query(int i, int l, int r) {
        if (isLazy(i, l, r)) return tree[i].sum;
        push(i);
        int m = tree[i].l + tree[i].r >> 1;
        int res = 0;
        if (l <= m) res += query(i << 1, l, r);
        if (m < r) res += query(i << 1 | 1, l, r);
        return res;
    }

    private static void push(int i) {
        if (tree[i].lazy) {
            touchLazy(i << 1);
            touchLazy(i << 1 | 1);
            tree[i].lazy = false;
        }
    }

    private static boolean isLazy(int i, int l, int r) {
        return l <= tree[i].l && tree[i].r <= r;
    }

    public static void touchLazy(int i) {
        tree[i].sum = tree[i].len - tree[i].sum;
        tree[i].lazy = !tree[i].lazy;
    }

    static class Node {
        int l, r, len;
        int sum;
        boolean lazy;

        public Node(int l, int r) {
            this.l = l;
            this.r = r;
            len = r - l + 1;
        }
    }
    static class Reader {
        StreamTokenizer in = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));

        public int nextInt() {
            try {
                in.nextToken();
            } catch (Exception ignored) {
            }
            return (int) in.nval;
        }
    }
}
